package com.cait.cait.service.impl;

import com.cait.cait.enums.VerificationGradeEnum;
import com.cait.cait.service.VerificationService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author Jirath
 * @date 2020/3/24
 * @description:
 */
@Service
public class VerificationServiceImpl implements VerificationService {
    @Autowired
    RedisTemplate redisTemplate;
    final Logger logger = LoggerFactory.getLogger(getClass());
    //验证码字符
    private String basicCode="ABCDEFGHIJKLMNOPRSTUVWXYZ123456";
    //缓存的名
    private String levelMem="membercode";
    private String levelMan="managecode";

    private String getRandomStr(){
        char[] randomStr=new char[8];
        for(int i=0;i<8;i++){
            randomStr[i]=basicCode.charAt((int)(Math.random()*basicCode.length()));
        }
        String str=String.valueOf(randomStr);
        return str;
    }

    /**
     * 判断是否有Code
     * @param level
     * @param code
     * @return
     */
    private Boolean isCode(String level,String code){
        Object msg=redisTemplate.opsForValue().get(level);
        if (msg==null){
            return false;
        }
        String rightCode=(String)msg;
        System.out.println(rightCode);
        Boolean isRight=code.equals(rightCode)? true : false;
        return isRight;
    }
    private String getCode(String level,Boolean refresh){
        if(!refresh && redisTemplate.hasKey(level)){
            return (String)redisTemplate.opsForValue().get(level);
        }
        String code= getRandomStr();
        redisTemplate.opsForValue().set(level,code);
        return code;
    }
    @Override
    public String getMemberVerificationCode() {
        String level=levelMem;
        String code=getCode(level,false);
        Subject subject= SecurityUtils.getSubject();
        logger.info(subject+"请求了成员授权Code"+code);
        return code;
    }

    @Override
    public String getManageVerificationCode() {
        String level=levelMan;
        String code=getCode(level,false);
        Subject subject= SecurityUtils.getSubject();
        logger.info(subject+"请求了管理授权Code"+code);
        return code;
    }
    @Override
    public Boolean isMemberVerification(String verificationCode) {
        return isCode(levelMem,verificationCode);
    }

    @Override
    public Boolean isManageVerification(String verificationCode) {
        return isCode(levelMan,verificationCode);
    }
    @Override
    public String refreshMemberCode() {
        String level=levelMem;
        String code=getCode(level,true);
        Subject subject= SecurityUtils.getSubject();
        logger.info(subject+"操作:刷新了成员授权Code:"+code);
        return code;
    }
    @Override
    public String refreshManageCode() {
        String level=levelMan;
        String code=getCode(level,true);
        Subject subject= SecurityUtils.getSubject();
        logger.info(subject+"操作:刷新了成员授权Code:"+code);
        return code;
    }
}
